package com.lsfb.signln.Statistics.DateStatistics;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lsfb.signln.Leave.PersonnelSelect.PersonnelSelectActivity;
import com.lsfb.signln.Login.LoginActivity;
import com.lsfb.signln.R;
import com.lsfb.signln.Utils.LittleUtils;
import com.lsfb.signln.Utils.LsfbYearPicker;
import com.lsfb.signln.Utils.MyProgressDialog;
import com.lsfb.signln.Utils.PopWindowManager;
import com.lsfb.signln.Utils.URLString;
import com.lsfb.signln.View.HorizontalChartView;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.HashMap;

@ContentView(R.layout.aty_statistics_date)
public class DateStatisticsActivity extends LsfbActivity {

    @ViewInject(R.id.aty_statistics_date_top)
    private LinearLayout aty_statistics_date_top;
    /**
     * 指定人员
     */
    @ViewInject(R.id.aty_text_statistics_date_user)
    private TextView tv_user;

    /**
     * 指定时间
     */
    @ViewInject(R.id.aty_text_statistics_date_time)
    private TextView tv_time;

    /**
     * 统计内容
     */
    @ViewInject(R.id.aty_text_statistics_date_content)
    private TextView tv_content;

    @ViewInject(R.id.horizontalChartView)
    private HorizontalChartView horizontalChartView;

    //人员类型
    private String usertype = "2";
    //下属人员
    private String users = null;
    //部门
    private String depars = null;

    /**
     * PopWindow管理类
     */
    private PopWindowManager replay;

    private MyProgressDialog dialog;

    //选择年份
    private LsfbYearPicker timePicker;

    private int page = 0;
    private String year = "2018";
    private int moldType = 0;

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_statistics_date_user, R.id.aty_statistics_date_time, R.id.aty_statistics_date_content})
    public void onClick(View view) {
        switch (view.getId()) {
            //指定人员
            case R.id.aty_statistics_date_user:
                startActivityForResult(new Intent(this, PersonnelSelectActivity.class), 0);
                break;
            //指定时间
            case R.id.aty_statistics_date_time:
                timePicker.showYearPickerView();
                break;
            //统计内容
            case R.id.aty_statistics_date_content:
                replay = new PopWindowManager(this, R.layout.pop_statistics_date_content) {
                    @Override
                    public void onViewLister(View contentView, final PopWindowManager manager) {

                        final RelativeLayout RelativeLayout = (RelativeLayout) contentView.findViewById(R.id.RelativeLayout);
                        //全部
                        final TextView content_all = (TextView) contentView.findViewById(R.id.pop_statistics_date_content_all);
                        //外勤定位
                        final TextView content_out = (TextView) contentView.findViewById(R.id.pop_statistics_date_content_out);
                        //内勤定位
                        final TextView content_inside = (TextView) contentView.findViewById(R.id.pop_statistics_date_content_inside);
                        //请假数
                        final TextView content_leave = (TextView) contentView.findViewById(R.id.pop_statistics_date_content_leave);
                        //补签数
                        final TextView content_retroactive = (TextView) contentView.findViewById(R.id.pop_statistics_date_content_retroactive);

                        RelativeLayout.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                manager.hideWindow();
                            }
                        });
                        //全部
                        content_all.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tv_content.setText("全部");
                                moldType = 0;
                                manager.hideWindow();
                            }
                        });
                        //外勤定位
                        content_out.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tv_content.setText("外勤定位");
                                moldType = 1;
                                manager.hideWindow();
                            }
                        });
                        //内勤定位
                        content_inside.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tv_content.setText("内勤定位");
                                moldType = 2;
                                manager.hideWindow();
                            }
                        });
                        //请假数
                        content_leave.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tv_content.setText("请假数");
                                moldType = 3;
                                manager.hideWindow();
                            }
                        });
                        //补签数
                        content_retroactive.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tv_content.setText("补签数");
                                moldType = 4;
                                manager.hideWindow();
                            }
                        });
                    }
                };
                replay.showWindows(aty_statistics_date_top, 0, 0);
                break;

        }
    }

    @Override
    public void initView() {
        LittleUtils.setsimpletitlebar(this, "统计");

        dialog = new MyProgressDialog(this, "加载中......");

        timePicker = new LsfbYearPicker(this);
        timePicker.SelectSureListenner(new LsfbYearPicker.ClickSureListener() {
            @Override
            public void ClickSure(String time) {
                tv_time.setText(time + "年");
                year = time;
                getDateStatistics();
            }
        });
        getDateStatistics();
    }

    /**
     * 获取按日期统计数据
     */
    private void getDateStatistics() {
        HashMap hashMap = new HashMap();
        hashMap.put("type", usertype);
        hashMap.put("year", year);
        hashMap.put("page", String.valueOf(page));
        if (usertype.equals("1")) {
            if (depars != null) {
                hashMap.put("deps", depars);
            }
            if (users != null) {
                hashMap.put("persons", users);
            }
        }
        hashMap.put("moldType", String.valueOf(moldType));
        dialog.showdialog();
        new BaseInternet().getData(URLString.DATESTATISTICS, hashMap, DateStatisticsBean.class, new DateStatisticsEvent<DateStatisticsBean>(), true);
    }

    @EventAnnotation
    public void eventResult(DateStatisticsEvent<DateStatisticsBean> event) {
        dialog.hidedialog();
        switch (event.getCode()) {
            case 200:
                if (event.getData().getMonth() != null && !event.getData().getMonth().isEmpty()) {
                    Float[] floats = new Float[event.getData().getMonth().size()];
                    for (int i = 0; i < floats.length; i++) {
                        floats[i] = event.getData().getMonth().get(i);
                    }
                    horizontalChartView.setRatio(floats);
                    horizontalChartView.update();
                }
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        } else {
            switch (requestCode) {
                case 0:
                    String type = data.getStringExtra("usertype");
                    tv_user.setText(type);
                    if (type.equals("我自己")) {
                        usertype = "0";
                    } else if (type.equals("自选")) {
                        usertype = "1";
                    } else if (type.equals("全部")) {
                        usertype = "2";
                        if (data.getStringExtra("users") != null) {
                            users = data.getStringExtra("users");
                        } else {
                            users = null;
                        }
                        if (data.getStringExtra("depars") != null) {
                            depars = data.getStringExtra("depars");
                        } else {
                            depars = null;
                        }
                    }
                    getDateStatistics();
                    break;
            }
        }
    }
}
